/**
 * SPDX-FileCopyrightText: 2023-2025 Sangfor Technologies Inc.
 * SPDX-License-Identifier: Mulan PSL v2
 */

/**
 * created by sch 2018/10/31
 */

import { extend } from 'src/util/extend';

/**
 * 获取字符串字节长度
 * @param {String} str
 * @param {Number} maxLength 字符串最大字节长度
 * @return {Object} 计算后的字节长度及超出最大字节长度时的index值
 */
function getStrLen (str, maxLength) {
    const CODE = 128;
    let realLength = 0;
    let len = str.length;
    let charCode = -1;
    let maxIndex = 0;
    for (let i = 0; i < len; i++) {
        charCode = str.charCodeAt(i);
        if (charCode >= 0 && charCode <= CODE) {
            realLength += 1;
        } else {

            // 如果是中文则长度加2
            realLength += Number('2');
        }
        if (realLength >= maxLength) {
            maxIndex = i;
            break;
        }
    }
    return maxIndex;
}

/**
 * 柱状图默认配置
 * @param {Object} options
 * @returns {Object} 组合后的对象
 * @public
 */
function getBarOptions (options = {}) {
    let baseOptions = {
        color: ['#4CB6EC'],
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'shadow'
            }
        },
        grid: {
            left: 'left',
            top: '15%',
            bottom: '6%',
            containLabel: true
        },
        xAxis: {
            type: 'category',
            data: [],
            axisLabel: {
                interval: 0,
                formatter: function (name) {
                    let ret = '';
                    let maxLength = 16;
                    let maxIndex = getStrLen(name, maxLength);
                    if (maxIndex) {
                        ret = name.substring(0, maxIndex) + '...';
                        return ret;
                    }
                    return name;
                }
            },
            axisTick: {
                alignWithLabel: true
            },
            axisLine: {
                lineStyle: {
                    color: '#80848F'
                }
            }
        },
        yAxis: {
            type: 'value',
            name: _('单位'),
            nameTextStyle: {
                color: '#80848F',
                fontSize: 12
            },
            axisLine: {
                show: false
            },
            axisTick: {
                show: false
            },
            axisLabel: {
                color: '#80848F'
            },
            splitLine: {
                lineStyle: {
                    type: 'dashed'
                }
            }
        },
        series: {
            name: _('漏洞数量'),
            type: 'bar',
            data: [],
            barMaxWidth: 70,
            label: {
                normal: {
                    show: true,
                    position: 'top',
                    color: '#333',
                    fontSize: 13
                }
            },
            animationDelay: function (idx) {
                return idx * Number('10');
            }
        },
        animationDelayUpdate: function (idx) {
            return idx * Number('5');
        }
    };
    return extend(baseOptions, options);
}

/**
 * 饼图默认配置
 * @param {Object} options
 * @returns {Object} 组合后的对象
 * @public
 */
function getPieOptions (options = {}) {
    let baseOptions = {
        title: {
            text: 0,
            left: 13,
            top: 20,
            textStyle: {
                fontFamily: 'DINPro-Regular',
                fontSize: 60,
                fontWeight: 'normal',
                color: '#4C637B'
            },
            subtext: '0',
            subtextStyle: {
                fontFamily: 'Microsoft YaHei',
                fontSize: 14
            }
        },
        tooltip: {
            trigger: 'item',
            formatter: '{a} <br/>{b}: {c} ({d}%)'
        },
        legend: {
            orient: 'vertical',
            itemWidth: 14,
            itemHeight: 14,
            itemGap: 15,
            data: []
        },
        series: [
            {
                name: _('漏洞数量'),
                type: 'pie',
                center: ['75%', '45%'],
                radius: ['40%', '60%'],
                avoidLabelOverlap: false,
                selectedMode: 'single',
                hoverAnimation: false,
                label: {
                    normal: {
                        show: false,
                        position: 'center'
                    },
                    emphasis: {
                        show: true,
                        textStyle: {
                            fontSize: '15',
                            fontWeight: 'bold'
                        }
                    }
                },
                labelLine: {
                    normal: {
                        show: false
                    }
                },
                stillShowZeroSum: false,
                data: [
                    { value: 0, name: _('高危漏洞') },
                    { value: 0, name: _('中危漏洞') },
                    { value: 0, name: _('低危漏洞') }
                ]
            }
        ],
        color: [
            '#ee5555',
            '#ff9800',
            '#4cb6ec'
        ]
    };
    return extend(baseOptions, options);
}

/**
 * 折线图默认配置
 * @param {Object} options
 * @returns {Object} 组合后的对象
 * @public
 */
function getLineOptions (options = {}) {
    let baseOptions = {
        grid: {
            top: 40,
            bottom: 40
        },
        title: {
            textStyle: {
                fontSize: '12px',
                color: '#FFF',
                fontFamily: 'Microsoft Yahei'
            }
        },
        xAxis: {
            type: 'category',
            boundaryGap: false,
            data: [],
            axisLine: {
                show: false
            },
            axisLabel: {
                color: 'rgba(255, 255, 255, .4)'
            },
            splitLine: {
                show: false
            }
        },
        yAxis: {
            type: 'value',
            splitNumber: 4
        },
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'cross',
                label: {
                    backgroundColor: '#4d8dd9',
                    fontSize: 12
                }
            },
            backgroundColor: '#fff',
            textStyle: {
                fontSize: 12,
                color: '#333'
            }
        }
    };
    return extend(baseOptions, options);
}
export default {
    getBarOptions,
    getPieOptions,
    getLineOptions,
    getStrLen
};
